#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/11/17 18:39
# @Author  : huidong.bai
# @File    : conftest.py
# @Software: PyCharm


import pytest
import requests
import json


def pytest_addoption(parser):
    parser.addoption("--task_id", action="store")
    parser.addoption("--system_version", action="store")
    parser.addoption("--sn", action="store")
    parser.addoption("--type_id", action="store")
    parser.addoption("--is_smoke", action="store")
    parser.addoption("--speakerVersion", action="store")
    parser.addoption("--romVersion", action="store")
    parser.addoption("--com", action="store")
    parser.addoption("--single_case_id", default="None", action="store")


@pytest.fixture(scope="session")
def task_id(request):
    return request.config.getoption("--task_id")


@pytest.fixture(scope="session")
def system_version(request):
    return request.config.getoption("--system_version")


@pytest.fixture(scope="session")
def sn(request):
    return request.config.getoption("--sn")


@pytest.fixture(scope="session")
def type_id(request):
    return request.config.getoption("--type_id")


@pytest.fixture(scope="session")
def is_smoke(request):
    return request.config.getoption("--is_smoke")


@pytest.fixture(scope="session")
def speakerVersion(request):
    return request.config.getoption("--speakerVersion")


@pytest.fixture(scope="session")
def romVersion(request):
    return request.config.getoption("--romVersion")


@pytest.fixture(scope="session")
def com(request):
    return request.config.getoption("--com")


@pytest.fixture(scope="session")
def single_case_id(request):
    return request.config.getoption("--single_case_id")


def pytest_generate_tests(metafunc):
    if "param" in metafunc.fixturenames:
        task_id = metafunc.config.getoption("task_id")
        is_smoke = metafunc.config.getoption("is_smoke")
        single_case_id = metafunc.config.getoption("single_case_id")

        # 单用例Case
        if single_case_id != 'None':
            response = requests.get(f'http://api.mydjangoproject.cn:8000/testcase/getCase/{single_case_id}/')
            testcases = [json.loads(response.text)]
        elif task_id and is_smoke:
            response = requests.get(f'http://api.mydjangoproject.cn:8000/testcase/getTaskAllCase?task_id={task_id}&is_smoke={is_smoke}')
            testcases = json.loads(response.text)
        else:
            testcases = []
        metafunc.parametrize("param", testcases)


# 全局夹具，目前支持单设备运行
@pytest.fixture(scope='session', autouse=True, name='data')
def session_fixture(type_id, system_version, sn, speakerVersion, romVersion, com):
    print('全局前置开始')

    # 在这里获取speaker_params的其他参数
    response = requests.get(f'http://api.mydjangoproject.cn:8000/speakerParams/{type_id}/')
    params = json.loads(response.text)

    # 整合基本参数
    data = {
        'system_version': system_version,
        'sn': sn,
        'type_id': type_id,
        'speakerVersion': speakerVersion,
        'romVersion': romVersion,
        'com': com,
        'url': params.get('url'),
        'product_secret': params.get('product_secret'),
        'productId': params.get('productId'),
        'osClientId': params.get('osClientId')
    }

    yield data

    # 将音箱的状态设置为0
    data = {
        "com": com,
        "run_status": 0
    }
    response = requests.post(f'http://api.mydjangoproject.cn:8000/speakerDetail/setStatus/', data=data)
    params = json.loads(response.text)
    print('全局后置结束：%s' % params)
